﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using LuaFramework;

public class TurnTableCtrl : MonoBehaviour
{
    public bool isRotate = false;
    public float rotateTimer = 0;
    public float time = 4;
    public string Id;
    public string prize = null;
    public bool isParse = false;
    // Use this for initialization
    void Start()
    {
        rotateTimer = 0;
        time = 4;
    }

    Tween tween = null;
    // Update is called once per frame
    void Update()
    {
        if (isRotate)
        {
            rotateTimer += Time.deltaTime;
            if (rotateTimer < time)
            {
                transform.Rotate(Vector3.back * 278 * Time.deltaTime);
            }
            else
            {
                tween = transform.DORotate(new Vector3(0, 0, -(360 + CalAngle(Id))), 6f, RotateMode.FastBeyond360);
                rotateTimer = 0;
                isRotate = false;
                OnRotated(Id);
                tween.OnKill(ShowPrize);
            }
        }
        //if (isRotate)
        //{
        //    if (!isParse)
        //    {
        //        transform.Rotate(Vector3.back * 278 * Time.deltaTime);
        //    }
        //    else
        //    {
        //        tween = transform.DORotate(new Vector3(0, 0, -(360 + CalAngle(Id))), 6f, RotateMode.FastBeyond360);
        //        isParse = false;
        //        isRotate = false;
        //        OnRotated(Id);
        //        tween.OnKill(ShowPrize);
        //    }
        //}
    }

    private float CalAngle(string id)
    {
        float angle = UnityEngine.Random.Range(2, 26);
        switch (Id)
        {
            case "0":
                angle = UnityEngine.Random.Range(335, 358);
                break;
            case "1":
                angle = UnityEngine.Random.Range(310, 330);
                break;
            case "2":
                angle = UnityEngine.Random.Range(251, 272);
                break;
            case "3":
                angle = UnityEngine.Random.Range(224, 247);
                break;
            case "4":
                angle = UnityEngine.Random.Range(196, 219);
                break;
            case "5":
                angle = UnityEngine.Random.Range(169, 191);
                break;
            case "6":
                angle = UnityEngine.Random.Range(143, 163);
                break;
            case "7":
                angle = UnityEngine.Random.Range(87, 106);
                break;
            case "8":
                angle = UnityEngine.Random.Range(29, 51);
                break;
            case "9":
                angle = UnityEngine.Random.Range(2, 26);
                break;
        }
        return angle;
    }

    private void ShowPrize()
    {
        Util.CallMethod("DailyTaskCtrl", "OnRotated", prize);
        prize = string.Empty;
    }

    private void OnRotated(string id)
    {
        switch (id)
        {
            case "0":
                prize = "恭喜你获得50枚钻石";
                break;
            case "1":
                prize = "恭喜你获得2000金币";
                break;
            case "2":
                prize = "很抱歉未中奖。下次一定中!";
                break;
            case "3":
                prize = "恭喜你获得2万金币";
                break;
            case "4":
                prize = "恭喜你获得1000金币";
                break;
            case "5":
                prize = "恭喜你获得5000金币";
                break;
            case "6":
                prize = "恭喜你获得1万金币";
                break;
            case "7":
                prize = "很抱歉未中奖。下次一定中";
                break;
            case "8":
                prize = "恭喜你获得500金币";
                break;
            case "9":
                prize = "很抱歉未中奖。下次一定中";
                break;
        }

    }
}
